package com.tinem.platform.module.starter.jms.enums;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

public class TopicEnum {
    public final static List<TopicEnum> TOPIC_ENUM = new ArrayList<>();

    public final static TopicEnum ORDER_NOTICE =new TopicEnum("ORDER_NOTICE");
    public final static TopicEnum ORDER_NOTICE_DELAY_5 =new TopicEnum("ORDER_NOTICE_DELAY_5");
    public final static TopicEnum ORDER_NOTICE_DELAY_25 =new TopicEnum("ORDER_NOTICE_DELAY_25");
    public final static TopicEnum ORDER_NOTICE_DELAY_125 =new TopicEnum("ORDER_NOTICE_DELAY_125");
    /**
     * 所有链接
     */
    public final static TopicEnum WEB_SOCKET_ALL_SESSION =new TopicEnum("WEB_SOCKET_ALL_SESSION");
    /**
     * 所有用户
     */
    public final static TopicEnum WEB_SOCKET_ALL_USER =new TopicEnum("WEB_SOCKET_ALL_USER");
    /**
     * 用户上线事件
     */
    public final static TopicEnum WEB_SOCKET_USER_ONLINE =new TopicEnum("WEB_SOCKET_USER_ONLINE");
    /**
     * 用户下线事件
     */
    public final static TopicEnum WEB_SOCKET_USER_OFFLINE =new TopicEnum("WEB_SOCKET_USER_OFFLINE");
    /**
     * 单个用户
     */
    public final static TopicEnum WEB_SOCKET_USER =new TopicEnum("WEB_SOCKET_USER");
    /**
     * 聊天室
     */
    public final static TopicEnum WEB_SOCKET_CHAT_ROOM =new TopicEnum("WEB_SOCKET_CHAT_ROOM");

    public TopicEnum(String name){
        TOPIC_ENUM.add(this);
        this.name = name;
    }
    public static List<TopicEnum> values(){
        return TOPIC_ENUM;
    }
    public static TopicEnum valueOf(String name){
        Optional<TopicEnum> messageCode = TOPIC_ENUM.stream().filter(mc -> mc.name().equalsIgnoreCase(name)).findFirst();
        if(messageCode.isPresent()){
            return messageCode.get();
        }
        throw new IllegalArgumentException("No enum "+name);
    }

    String name;

    public String name(){
        return name;
    }

    public String getTopic(String active,String applicationName,String... args){
        if(args.length == 0){
            return active+"_"+applicationName+"_"+this.name().toLowerCase(Locale.ROOT);
        }
        return active+"_"+applicationName+"_"+this.name().toLowerCase(Locale.ROOT)+"_"+ StrUtil.join("_",args).toLowerCase(Locale.ROOT);
    }
    public static String getUserTopic(String active,String applicationName,String userId){
        return TopicEnum.WEB_SOCKET_USER.getTopic(active,applicationName,userId);
    }
    public static String getChatRoomTopic(String active,String applicationName,String chatRoomId){
        return TopicEnum.WEB_SOCKET_CHAT_ROOM.getTopic(active,applicationName,chatRoomId);
    }
    public static String getAllUser(String active,String applicationName){
        return TopicEnum.WEB_SOCKET_ALL_USER.getTopic(active,applicationName);
    }
    public static String getAllSession(String active,String applicationName){
        return TopicEnum.WEB_SOCKET_ALL_SESSION.getTopic(active,applicationName);
    }
    ;
}
